//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <bsp.h>
#include <init.h>
#include "pic.h"
#include <timer.h>

EXTERN_C DECL_CODEINIT void CDECL InitPIC()
{
    Outl(_ICCR, _DIM);
    Outl(_ICLR, 0);
    Outl(_ICLR2, 0);

    BspMaskAllIrqs(FALSE);
}

EXTERN_C irqs_t CDECL BspGetIrqs()
{
    return (irqs_t)Inl(_ICIP) | ((irqs_t)Inl(_ICIP2) << 32);
}

INLINE void EndOfInterrupt(irq_t irq)
{
    switch (irq) {
        case PIT_Irq: EndOfPITInterrupt(); break;
    // BUG: Not implement
    }
}

STATIC irqs_t s_irqMask = 0;

EXTERN_C void CDECL BspMaskIrqs(irqs_t irqs, bool_t bEnable)
{
    if (bEnable)
        s_irqMask |= irqs;
    else
        s_irqMask &= ~irqs;

    if (irqs & 0x00000000ffffffffll) Outl(_ICMR, s_irqMask);
    if (irqs & 0xffffffff00000000ll) Outl(_ICMR2, s_irqMask >> 32);
}

EXTERN_C void CDECL BspMaskAllIrqs(bool_t bEnable)
{
    s_irqMask = bEnable ? 0xffffffffffll : 0x0000000000ll;

    Outl(_ICMR, s_irqMask);
    Outl(_ICMR2, s_irqMask >> 32);
}

EXTERN_C void CDECL BspAcknowledgeIrq(irq_t irq)
{
    EndOfInterrupt(irq);
}
